package com.shiro.work1.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shiro.work1.domain.Album;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class JsonUtils {
    public static List<HashMap<String, Object>> getAlbum(String json){
        List<HashMap<String, Object>> dataList =new ArrayList<>();
        JSONObject rootObject = JSON.parseObject(json);
//        JSONObject paramzObject = rootObject.getJSONObject("paramz");
        //获取songs中所有内容
        JSONArray songsArray = rootObject.getJSONArray("songs");
        for (int i = 0; i < songsArray.size(); i++) {
            //创建Map返回数据
            HashMap<String, Object> map = new HashMap<>();
            //获取单个song的内容
            JSONObject song = songsArray.getJSONObject(i);
            //获取song中ar内容获取author
            JSONArray ar = song.getJSONArray("ar");
            for (int j = 0; j < ar.size(); j++) {
                JSONObject jObject = ar.getJSONObject(j);
                String author = jObject.getString("name");
                map.put("author",author);
            }
            //获取song中al中id和name
            JSONObject al = song.getJSONObject("al");
            String id = al.getString("id");
            map.put("id",id);
            String name = al.getString("name");
            map.put("name",name);
            //获取song中歌曲名
            String singleSong = song.getString("name");
            map.put("song",singleSong);
            dataList.add(map);
        }
        return dataList;
    }
}
